﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Channels;

namespace BilingualSentSearchEngine
{
    public class FaultErrorHandler : IErrorHandler
    {
        public bool HandleError(Exception error)
        {
            CommunicationException fe = error as CommunicationException;

            if (fe != null)
            {
                System.Diagnostics.EventLog.WriteEntry("Bilingual Sentence Search Engine Exception", "Communication Exception:\nMessage:" + fe.Message + "\n-----------\nInner Exception:" + fe.InnerException.ToString() + "\n------------\nSource: " + fe.Source + "\n------------\nStack Trace:" + fe.StackTrace + "\n-----------\nTargetSite" + fe.TargetSite.ToString());
            }
            else
            {
                System.Diagnostics.EventLog.WriteEntry("Bilingual Sentence Search Engine Exception", "Uncaught Exception\nMessage:\n" + error.Message + "\n---------------\nSource:" + error.Source + "\n-------------\nStack Trace:" + error.StackTrace);
            }
            return false;
        }

        public void ProvideFault(Exception error, System.ServiceModel.Channels.MessageVersion version, ref System.ServiceModel.Channels.Message fault)
        {
            //if (fault == null)
            //{
            //    FaultException<ReceiverFaultDetail> fe = new FaultException<ReceiverFaultDetail>(new ReceiverFaultDetail(error.Message, true), error.Message, FaultCode.CreateReceiverFaultCode(new FaultCode("Configuration")));
            //    MessageFault mf = fe.CreateMessageFault();

            //    fault = Message.CreateMessage(version, mf, fe.Action);

            //}
        }
    }
}
